<?php 
	class Test_volumes extends CI_Model{

		function __construct(){
			parent::__construct();
		}

		function get_test_volume_list($limit = 0, $offset = 10, $conditions = array()){
            $where = " WHERE 1=1";
            if(!empty($conditions['subject_id'])){
                $where .= " AND tv.subject_id = '{$conditions['subject_id']}' ";
            }

            if(!empty($conditions['type'])){
                $where .= " AND tvp.type = '{$conditions['type']}' ";
            }

            if(!empty($conditions['school_id'])){
                $where .= " AND tvp.school_id = '{$conditions['school_id']}' ";
            }

            $sql = "SELECT SQL_CALC_FOUND_ROWS tvp.*, tv.name,tv.id FROM {$this->db->dbprefix}tk_test_volumes_publish AS tvp ";
            $sql .= "LEFT JOIN {$this->db->dbprefix}tk_test_volumes AS tv ON(tvp.volume_id = tv.id) ";
            $sql .= "$where ORDER BY tvp.id DESC LIMIT $limit, $offset ";
            $result = $this->db->query($sql)->result_array();
            $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
            return array("count" => $row_num['row_num'], "result" => $result);
        }

        function get_volume_publish_list($limit = 0, $offset = 0, $conditions = array()){
            $where = "WHERE 1=1";
            if(!empty($conditions['type'])){
                $where .= " AND tv.type = '{$conditions['type']}'";
            }
            if(!empty($conditions['subject_id'])){
                $where .= " AND tv.subject_id = '{$conditions['subject_id']}'";
            }
            if(!empty($conditions['home_work_name'])){
                $where .= ' AND tv.name like "%'.$conditions['home_work_name'].'%"';
            }
            $sql = "SELECT SQL_CALC_FOUND_ROWS tv.*, s.name AS subject_name FROM {$this->db->dbprefix}tk_test_volumes AS tv ";
            $sql .= "LEFT JOIN {$this->db->dbprefix}b_subject AS s ON(s.id=tv.subject_id) ";
            $sql .= "$where ORDER BY tv.id DESC ";
            if($limit > 0 || $offset > 0){
                $sql .= "LIMIT $limit, $offset";
            }
            $result = $this->db->query($sql)->result_array();
            if($limit > 0 || $offset > 0){
                $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
                return array("count" => $row_num['row_num'], "result" => $result);
            }else{
                return $result;
            }
        }

        function get_volume_publish_history($limit = 0, $offset = 0, $conditions = array()){
            $where = " WHERE home_work_h.uid='{$this->user_data['id']}'";
            if(!empty($conditions['type'])){
                $where .= " AND home_work_h.type = '{$conditions['type']}'";
            }
            
            if(!empty($conditions['home_work_id'])){
                $where .= " AND home_work_h.data_id = '{$conditions['home_work_id']}'";
            }

            if(!empty($conditions['name'])){
                $where .= " AND s.name like '%{$conditions['name']}%'";
            }
            
            $sql = "SELECT SQL_CALC_FOUND_ROWS home_work_h.*, s.name FROM {$this->db->dbprefix}tk_test_volume_historys AS home_work_h ";
            $sql .= "LEFT JOIN {$this->db->dbprefix}o2o_student AS s ON(s.user_id=home_work_h.uid) ";
            $sql .= "$where ORDER BY id DESC LIMIT $limit, $offset ";
            $result = $this->db->query($sql)->result_array();
            $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
            return array("count" => $row_num['row_num'], "result" => $result);
        }
	}